package com.ptmall.app.utils;


import android.annotation.SuppressLint;
import android.text.format.DateUtils;

import com.ptmall.app.App;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class TimeUtil {

    /**
     * 2016-12-12 周一
     *
     * @param time
     * @return
     */
    public static String getDate(long time) {

        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");

        String week = DateUtils.formatDateTime(App.getInstance().getApplicationContext(), time, DateUtils.FORMAT_SHOW_WEEKDAY).toString();
        return new StringBuilder().append(formate.format(date)).append(" ").append(week).toString();
    }

    //时间戳转字符串
    public static String getStrTime(String timeStamp){
        try {
            if(timeStamp!=null && "".equals(timeStamp) && timeStamp.length()<5){
                return "";
            }
            String timeString = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            long  l = Long.valueOf(timeStamp);
            timeString = sdf.format(new Date(l* 1000L));//单位秒
            return timeString;
        }catch (Exception e){
            e.printStackTrace();
            return timeStamp;
        }

    }
    //时间戳转字符串
    public static String getStrTime2(String timeStamp){
        try {
            if(timeStamp!=null && "".equals(timeStamp) && timeStamp.length()<5){
                return "";
            }
            String timeString = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            long  l = Long.valueOf(timeStamp);
            timeString = sdf.format(new Date(l* 1000L));//单位秒
            return timeString;
        }catch (Exception e){
            e.printStackTrace();
            return timeStamp;
        }

    }


    public static String getYYYYMMdd(long time) {
        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        return formate.format(date);
    }

    public static String getMMddHHMM(long time) {
        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("MM-dd HH:mm");
        return formate.format(date);
    }

    public static String getYYYYMMddHHMM(long time) {
        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return formate.format(date);
    }

    public static String getYYYYMMddHHMMSS(long time) {
        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formate.format(date);
    }

    /**
     * 12:32
     *
     * @param time
     * @return
     */
    public static String getTimeS(long time) {

        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("HH:mm:ss");
        return formate.format(date);
    }

    /**
     * 12:32
     *
     * @param time
     * @return
     */
    public static String getTime(long time) {

        Date date = new Date(time);
        SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
        return formate.format(date);
    }



    /**
     * 判断两个时间点是否在同一天
     *
     * @param when1
     * @param when2
     * @return
     */
    public static boolean isSameDay(long when1, long when2) {

        Date date1 = new Date(when1);
        Date date2 = new Date(when2);
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        if (formate.format(date1).equals(formate.format(date2))) {
            return true;
        }
        return false;
    }
    public static String dateToStamp(String s) {
        try {

            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        }catch (Exception e)
        {
            return s;
        }
    }
    public static String dateToStamp2(String s) {
        try {

            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        }catch (Exception e)
        {
            return s;
        }
    }

    public static String getTimestampString(Date var0) {
        String var1 = null;
        String var2 = Locale.getDefault().getLanguage();
        boolean var3 = var2.startsWith("zh");
        long var4 = var0.getTime();
        if(isSameDay(var4)) {
            if(var3) {
                var1 = "aa hh:mm";
            } else {
                var1 = "hh:mm aa";
            }
        } else if(isYesterday(var4)) {
            if(!var3) {
                return "Yesterday " + (new SimpleDateFormat("hh:mm aa", Locale.ENGLISH)).format(var0);
            }

            var1 = "昨天aa hh:mm";
        } else if(var3) {
            var1 = "M月d日aa hh:mm";
        } else {
            var1 = "MMM dd hh:mm aa";
        }

        return var3?(new SimpleDateFormat(var1, Locale.CHINESE)).format(var0):(new SimpleDateFormat(var1, Locale.ENGLISH)).format(var0);
    }

    public static boolean isCloseEnough(long var0, long var2) {
        long var4 = var0 - var2;
        if(var4 < 0L) {
            var4 = -var4;
        }

        return var4 < 30000L;
    }

    @SuppressLint({"DefaultLocale"})
    public static String toTime(int var0) {
        var0 /= 1000;
        int var1 = var0 / 60;
        boolean var2 = false;
        if(var1 >= 60) {
            int var4 = var1 / 60;
            var1 %= 60;
        }

        int var3 = var0 % 60;
        return String.format("%02d:%02d", new Object[]{Integer.valueOf(var1), Integer.valueOf(var3)});
    }

    private static boolean isYesterday(long var0) {
        TimeInfo var2 = getYesterdayStartAndEndTime();
        return var0 > var2.getStartTime() && var0 < var2.getEndTime();
    }

    public static TimeInfo getYesterdayStartAndEndTime() {
        Calendar var0 = Calendar.getInstance();
        var0.add(Calendar.DATE, -1);
        var0.set(Calendar.HOUR_OF_DAY, 0);
        var0.set(Calendar.MINUTE, 0);
        var0.set(Calendar.SECOND, 0);
        var0.set(Calendar.MILLISECOND, 0);
        Date var1 = var0.getTime();
        long var2 = var1.getTime();
        Calendar var4 = Calendar.getInstance();
        var4.add(Calendar.DATE, -1);
        var4.set(Calendar.HOUR_OF_DAY, 23);
        var4.set(Calendar.MINUTE, 59);
        var4.set(Calendar.SECOND, 59);
        var4.set(Calendar.MILLISECOND, 999);
        Date var5 = var4.getTime();
        long var6 = var5.getTime();
        TimeInfo var8 = new TimeInfo();
        var8.setStartTime(var2);
        var8.setEndTime(var6);
        return var8;
    }

    private static boolean isSameDay(long var0) {
        TimeInfo var2 = getTodayStartAndEndTime();
        return var0 > var2.getStartTime() && var0 < var2.getEndTime();
    }

    public static TimeInfo getTodayStartAndEndTime() {
        Calendar var0 = Calendar.getInstance();
        var0.set(Calendar.HOUR_OF_DAY, 0);
        var0.set(Calendar.MINUTE, 0);
        var0.set(Calendar.SECOND, 0);
        var0.set(Calendar.MILLISECOND, 0);
        Date var1 = var0.getTime();
        long var2 = var1.getTime();
        Calendar var4 = Calendar.getInstance();
        var4.set(Calendar.HOUR_OF_DAY, 23);
        var4.set(Calendar.MINUTE, 59);
        var4.set(Calendar.SECOND, 59);
        var4.set(Calendar.MILLISECOND, 999);
        Date var5 = var4.getTime();
        long var6 = var5.getTime();
        TimeInfo var8 = new TimeInfo();
        var8.setStartTime(var2);
        var8.setEndTime(var6);
        return var8;
    }
    public static class TimeInfo {
        private long startTime;
        private long endTime;

        public TimeInfo() {
        }

        public long getStartTime() {
            return this.startTime;
        }

        public void setStartTime(long var1) {
            this.startTime = var1;
        }

        public long getEndTime() {
            return this.endTime;
        }

        public void setEndTime(long var1) {
            this.endTime = var1;
        }
    }
}
